
class I2Char(CharFrame):
    result = None
    def __init__(self, path, limitSize=-1, fill=False, wrap=False):
        self.genCharImage(path, limitSize, fill, wrap)
    def genCharImage(self, path, limitSize=-1, fill=False, wrap=False):
        cap = cv2.VideoCapture(path)
        #img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        #print(cap.read()[1])
        rawFrame = cv2.cvtColor(cap.read()[1],cv2.COLOR_BGR2GRAY)
        frame = self.convert(rawFrame, os.get_terminal_size(), fill=True)
        self.result =frame
        #cap.release()
        # 给self.result 字符画字符串赋值
        #pass

    def show(self, stream = 2):
        if self.result is None:
            return
        if stream == 1 and os.isatty(sys.stdout.fileno()):
            self.streamOut = sys.stdout.write
            self.streamFlush = sys.stdout.flush
        elif stream == 2 and os.isatty(sys.stderr.fileno()):
            self.streamOut = sys.stderr.write
            self.streamFlush = sys.stderr.flush
        elif hasattr(stream, 'write'):
            self.streamOut = stream.write
            self.streamFlush = stream.flush
        self.streamOut(self.result)
        self.streamFlush()
        self.streamOut('\n')

if __name__ == '__main__':
    # import argparse
    # # 设置命令行参数
    # parser = argparse.ArgumentParser()
    # # 获取参数
    # args = parser.parse_args()
    # print(args.file)
    # i2char = I2Char(args.file)
    # # 如果有.txt字符动画文件，就直接导入
    # # 否则正常转码播放
    # i2char.show()

    '''
        通过命令行调用图片转字符画功能 ,命令行执行 Python v2char.py t3.jpeg,输出字符画
    '''
    import argparse

    # 命令行输入参数处理
    parser = argparse.ArgumentParser()
    parser.add_argument('file', help='Image file, .jpeg or .png.')  # 输入文件
    # parser.add_argument('-o', '--output', nargs='?', const='char_image.txt',
    #                     help='Export char_image file.')  # 输出文件
    # 获取参数
    args = parser.parse_args()
    # 实例化I2Char类对象为i2char, 实例化的时候初始参数会直接调用genCharImage方法生成字符画
    i2char = I2Char(args.file)
    # 在终端打印出字符画
    i2char.show()